package com.mdoffice.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.mdoffice.beans.PatientBean;
import com.mdoffice.dao.interfaces.PatientDAO;
import com.mdoffice.utils.DBConnection;
import com.mdoffice.vo.PatientListVo;

public class PatientDAOImpl implements PatientDAO {
	Connection con = null;
	ResultSet rs = null;
	PreparedStatement ps = null;
	Statement st = null;

	public int insert(PatientBean pbean) {
		// TODO Auto-generated method stub
		int patientID = 0;
		int i = 0;
		// PatientBean pbean=new PatientBean();
		con = DBConnection.getConnection();
		try {
			System.out.println("connected");
			st = con.createStatement();
			con.setAutoCommit(false);
			rs = st.executeQuery("select max(patientid) from patient");
			if (rs.next()) {
				patientID = rs.getInt(1);
			} else {
				patientID = 0;
			}
			patientID++;
			pbean.setPatientID(patientID);
			ps = con.prepareStatement("insert into patient values(?,?,?,?,?)");
			ps.setInt(1, pbean.getPatientID());
			ps.setString(2, pbean.getPname());
			ps.setString(3, pbean.getPaddr());
			ps.setString(4, pbean.getDiesease());
			ps.setString(5, pbean.getDrugs());
			i = ps.executeUpdate();
			System.out.println("inserted successfully");
			if (i == 1) {
				con.commit();
			} else {
				con.rollback();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				st.close();
				ps.close();
				con.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return patientID;
	}

	public PatientListVo select(int PatientID) {
		// TODO Auto-generated method stub
		System.out.println("patientID--Impl--" + PatientID);
		PatientListVo pvo = new PatientListVo();
		try {
			con = DBConnection.getConnection();
			st = con.createStatement();
			rs = st.executeQuery("select * from patient where patientID="
					+ PatientID);
			while (rs.next()) {
				pvo.setPatientID(rs.getInt("patientID"));
				pvo.setPname(rs.getString("pname"));
				pvo.setPaddr(rs.getString("paddr"));
				pvo.setDiesease(rs.getString("diesease"));
				pvo.setDrugs(rs.getString("drugs"));
			}
			System.out.println("Value Object Setted");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return pvo;
	}

	public List selectAllPatient() {
		// TODO Auto-generated method stub
		List l = new ArrayList();
		con = DBConnection.getConnection();
		try {
			st = con.createStatement();
			rs = st.executeQuery("select * from Patient");
			System.out.println("Before Set the List");
			int i = 0;
			while (rs.next()) {
				PatientListVo pvo = new PatientListVo();
				pvo.setPatientID(rs.getInt("patientID"));
				pvo.setPname(rs.getString("pname"));
				pvo.setPaddr(rs.getString("paddr"));
				pvo.setDiesease(rs.getString("diesease"));
				pvo.setDrugs(rs.getString("drugs"));
				l.add(pvo);
				i++;
			}
			System.out.println("After Set The List");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return l;
	}
}